﻿using UnityEngine;

public class ControllerBase : IController
{
	protected string _windowPath;
	protected GameObject _monoObject;
	protected MonoBase _mono;

	virtual public bool Create()
	{
		if (_monoObject != null) return false;
		Init();
		return true;
	}

	virtual protected void Init()
	{
		//加载并实例化当前窗口
		_monoObject = Object.Instantiate(Resources.Load(_windowPath), Vector3.zero, Quaternion.identity) as GameObject;
		Canvas canvas = _monoObject.GetComponent<Canvas>();
		canvas.renderMode = RenderMode.ScreenSpaceCamera;
		canvas.worldCamera = CameraManager.GetInstance().UICamera;
		//这一句改变GameObject名字纯属强迫症，看着顺眼，可以无视
		_monoObject.name = _windowPath.Substring(_windowPath.LastIndexOf("/") + 1);
		_mono = _monoObject.GetComponent<MonoBase>();
		if (_mono != null) _mono.SetDepth();
	}

	virtual public void Destroy()
	{
		GameTools.Destroy(_monoObject);
		_monoObject = null;
	}

	public GameObject MonoObject
	{
		get { return _monoObject; }
	}
}